<?php 

    // Connect Database
	require('sessao.inc');    
	
    // Get parameters from Array
    $cityid = !empty($_GET['id'])
              ?intval($_GET['id']):0;
    
    // if there is no city selected by GET, fetch all rows    
    $query = "SELECT codigo, nome FROM tb_cidades";
    
    //  else concatenate query with city id in order to filter.
    if($cityid>0) $query.=" WHERE estado = '$cityid'"; 
    else $query.="";
    
    //  fetch the results
    $result = mysql_query($query);
    $items = array();
    if($result && mysql_num_rows($result)>0) {
        while($row = mysql_fetch_array($result)) {
            $items[] = array( $row[0], utf8_encode($row[1]) );
        }        
    }    
    echo json_encode($items); 
?> 